﻿<link href="~/Content/standard_form_nhaplieu.css" rel="stylesheet" />

<script type="text/javascript">
                                     @functions{
                                         public string GetAntiForgeryToken()
                                         {
                                             string cookieToken, formToken;
                                             AntiForgery.GetTokens(null, out cookieToken, out formToken);
                                             return cookieToken + "," + formToken;
                                         }
                                     }

    $(document).ready(function () {
        $("#DISTRICT_CODE").attr('disabled', 'disabled');
        $("#COMMUNE_CODE").attr('disabled', 'disabled');
        $("#PROVINCE_CODE").change(function () {

            $("#divLoading").show();
            $("#PROVINCE_CODE").attr('disabled', 'disabled');
            $("#DISTRICT_CODE").attr('disabled', 'disabled');
            $("#COMMUNE_CODE").attr('disabled', 'disabled');
            $("#btnGenerateCode").attr('disabled', 'disabled');

            $("#DISTRICT_CODE").empty();
            $("#COMMUNE_CODE").empty();
            $("#DISTRICT_CODE").append('<option value="' + '-1' + '">' +
                 '-- Chọn Huyện --' + '</option>');
            $("#COMMUNE_CODE").append('<option value="' + '-1' + '">' +
                 '-- Chọn Xã --' + '</option>');
            $.ajax({
                type: 'POST',
                url: '@Url.Action("GetAdministrativeUnit1")',
                headers: {
                    'VerificationToken': '@GetAntiForgeryToken()'
                },
                dataType: 'json',

                data: {
                    code: $("#PROVINCE_CODE").val(),
                    level: '2'
                },

                success: function (states) {

                    $.each(states, function (i, state) {
                        $("#DISTRICT_CODE").append('<option value="' + state.Value + '">' +
                             state.Text + '</option>');

                    });
                },
                error: function (ex) {
                    alert('Failed to retrieve states.' + ex);
                },
                complete: function () {
                    $("#divLoading").hide();
                    $("#PROVINCE_CODE").removeAttr('disabled');
                    $("#DISTRICT_CODE").removeAttr('disabled');
                    $("#btnGenerateCode").removeAttr('disabled');
                }
            });
            return false;
        })
        $("#DISTRICT_CODE").change(function () {
            $("#divLoading").show();
            $("#PROVINCE_CODE").attr('disabled', 'disabled');
            $("#DISTRICT_CODE").attr('disabled', 'disabled');
            $("#COMMUNE_CODE").attr('disabled', 'disabled');
            $("#btnGenerateCode").attr('disabled', 'disabled');

            $("#COMMUNE_CODE").empty();
            $("#COMMUNE_CODE").append('<option value="' + '-1' + '">' +
                '-- Chọn Xã --' + '</option>');
            $.ajax({
                type: 'POST',
                url: '@Url.Action("GetAdministrativeUnit1")', // we are calling json method
                headers: {
                    'VerificationToken': '@GetAntiForgeryToken()'
                },
                dataType: 'json',
                data: {
                    code: $("#DISTRICT_CODE").val(),
                    level: '3'
                },

                success: function (states) {

                    $.each(states, function (i, state) {
                        $("#COMMUNE_CODE").append('<option value="' + state.Value + '">' +
                             state.Text + '</option>');

                    });
                },
                error: function (ex) {
                    alert('Failed to retrieve states.' + ex);
                },
                complete: function () {
                    $("#divLoading").hide();
                    $("#PROVINCE_CODE").removeAttr('disabled');
                    $("#DISTRICT_CODE").removeAttr('disabled');
                    $("#COMMUNE_CODE").removeAttr('disabled');
                    $("#btnGenerateCode").removeAttr('disabled');
                }
            });
            return false;
        });


        $('#btnGenerateCode').click(function () {
            var tinh = $('#PROVINCE_CODE').val();
            var huyen = $('#DISTRICT_CODE').val();
            var xa = $('#COMMUNE_CODE').val();
            $('#divMessage').text('');
            var thongbao = 'Bạn có chắc chắn muốn tạo mã đăng nhập cho đơn vị đã chọn không?';
            if (tinh == '-1') {
                thongbao = 'Bạn có chắc chắn muốn tạo mã đăng nhập cho tất cả hộ dân cư đăng ký phương thức Online hay không?';
            }
            bootbox.dialog({
                title: "Thông báo",
                message: thongbao,
                buttons: {
                    success: {
                        label: "<i class='fa fa-save'></i> Đồng ý",
                        className: "btn btn-sm btn-primary",
                        callback: function () {
                            var dataSend = {
                                'PROVINCE_CODE': tinh,
                                'DISTRICT_CODE': huyen,
                                'COMMUNE_CODE': xa,
                                'IsResetAllGenerate': $('#IsResetAllGenerate').is(':checked')
                            };
                            $("#divLoading").show();
                            $("#PROVINCE_CODE").attr('disabled', 'disabled');
                            $("#DISTRICT_CODE").attr('disabled', 'disabled');
                            $("#COMMUNE_CODE").attr('disabled', 'disabled');
                            $("#btnGenerateCode").attr('disabled', 'disabled');
                            var jqXHR = $.ajax({
                                url: '@Url.Action("GenerateCode","Online")',
                                dataType: 'json',
                                method: 'post',
                                data: dataSend,
                                headers: {
                                    'VerificationToken': '@GetAntiForgeryToken()'
                                },

                                success: function myfunctionSuccess(data, textStatus, jqXHR) {
                                    $('#divMessage').text(data);
                                },
                                complete: function () {
                                    $("#divLoading").hide();
                                    $("#PROVINCE_CODE").removeAttr('disabled');
                                    $("#DISTRICT_CODE").removeAttr('disabled');
                                    $("#COMMUNE_CODE").removeAttr('disabled');
                                    $("#btnGenerateCode").removeAttr('disabled');
                                }

                            });
                        }
                    },
                    danger: {
                        label: "<i class='fa fa-reply'></i> Bỏ qua",
                        className: "btn btn-sm btn-default",
                        callback: function () {
                        }
                    }
                }
            });
        });
    });
</script>

@{
    ViewBag.Title = "Sinh mã đăng nhập";
    Layout = "~/Views/Shared/_LayoutAdmin.cshtml";
}


<div class="page-bar">
    <ul class="page-breadcrumb" style="padding: 4px;">
        <li>
            <i class="fa fa-home"></i>
            <a href="#"> Sinh mã đăng nhập</a>
        </li>
    </ul>
</div>

@model DHQGHN.PM2.ViewModels.Online.OnlineViewModels
@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    <div class="row">
        <div class="col-md-6">
            <fieldset style="border-style: hidden;">
                <div class="row">
                    <div class="col-md-2">
                        @Html.LabelFor(m => m.PROVINCE_CODE, new { @class = "text-center" })
                    </div>
                    <div class="col-md-10">
                        @Html.DropDownListFor(m => m.PROVINCE_CODE, ViewData["DISTRICT_CODE"] as List<SelectListItem>,
                                                      new { @class = "form-control " })
                    </div>
                </div>
                <div class="row" style="margin-top:5px;">
                    <div class="col-md-2">
                        @Html.LabelFor(m => m.DISTRICT_CODE, new { @class = "text-center" })
                    </div>
                    <div class="col-md-10">
                        @Html.DropDownListFor(m => m.DISTRICT_CODE, new SelectList(string.Empty, "Value", "Text"), "-- Chọn huyện --",
                                                     new { @class = "form-control " })
                    </div>

                </div>
                <div class="row" style="margin-top:5px;">
                    <div class="col-md-2">
                        @Html.LabelFor(m => m.COMMUNE_CODE, new { @class = "text-center" })
                    </div>
                    <div class="col-md-10">
                        @Html.DropDownListFor(m => m.COMMUNE_CODE, new SelectList(string.Empty, "Value", "Text"), "-- Chọn xã --",
                                                    new { @class = "form-control " })
                    </div>
                </div>
                <div class="row" style="margin-top:5px;">
                    <div class="col-md-2">
                    </div>
                    <div class="col-md-10">
                        @Html.CheckBoxFor(m => m.IsResetAllGenerate)<span>  </span>
                        @Html.LabelFor(m => m.IsResetAllGenerate, new { @class = "form-label" })
                    </div>

                </div>
            </fieldset>
            <div class="row msg_event">
                <div class="col-md-10 text-danger" id="divMessage">
                </div>
            </div>
            <div class="row" style="">
                <div class="col-md-2">
                </div>
                <div class="col-md-10">
                    <button type="button" id="btnGenerateCode" class="btn btn-sm btn-default">
                        <i class="fa fa-qrcode"></i> Tạo mã đăng nhập
                    </button>
                </div>
            </div>
        </div>
    </div>
}
<script src="~/Scripts/index.js"></script>

